/* 
 * File:   SaveOnExitCommand.cpp
 * Author: Gab'z
 * 
 * Created on 13 juin 2012, 13:28
 */

#include "SaveOnExitCommand.h"
#include "../../Context.h"
#include " ../../../../memento/BooleanParameterMemento.h"

namespace calculator {

    SaveOnExitCommand::SaveOnExitCommand(const std::string name) : ParametersCommand(name) {
    }

    SaveOnExitCommand::SaveOnExitCommand(const SaveOnExitCommand& orig) : ParametersCommand(orig._name) {
    }

    SaveOnExitCommand::~SaveOnExitCommand() {
    }

    SaveOnExitCommand* SaveOnExitCommand::clone() const {
        return new SaveOnExitCommand(_name);
    }

    void SaveOnExitCommand::apply(Parameters* parameters) const {
        parameters->setDisplayKeyboard(!parameters->getDisplayKeyboard());
    }

    const Memento* SaveOnExitCommand::createMemento() const throw (CommandException) {
        return new BooleanParameterMemento(clone(), Context::getInstance()->getParameters()->getDisplayKeyboard());
    }

    void SaveOnExitCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const BooleanParameterMemento* booleanParameterMemento = dynamic_cast<const BooleanParameterMemento*> (memento);
        if (booleanParameterMemento == 0) {
            std::string str = "Bad Memento type, expected : BooleanParameterMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getParameters()->setDisplayKeyboard(booleanParameterMemento->_boolean);
    }
}